/******************************************************************************/
/*  mbport.h                                                                  */
/*                                                                            */
/*  FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. */
/*  Copyright (c) 2006 Christian Walter <wolti@sil.at>                        */
/*  All rights reserved.                                                      */
/******************************************************************************/

#ifndef __MB_PORT_H
#define __MB_PORT_H


#include "data_types.h"


#ifdef __cplusplus
  PR_BEGIN_EXTERN_C
#endif


/******************************************************************************/
/*      Type definitions                                                      */
/******************************************************************************/
typedef enum
{
    MB_EV_READY,                // Startup finished
    MB_EV_FRAME_RECEIVED,       // Frame received
    MB_EV_FRAME_SENT            // Frame sent
} eMB_EVENT_TYPE;


typedef enum
{
    MB_PAR_NONE,                // No parity
    MB_PAR_ODD,                 // Odd parity
    MB_PAR_EVEN                 // Even parity
} eMB_PARITY;


/******************************************************************************/
/*      Supporting functions                                                  */
/******************************************************************************/
#define MODBUS_RX_USB_EVENT      (0x00000001)
#define MODBUS_TX_USB_EVENT      (0x00000002)

#define MODBUS_POLL_EVENT        (0x00000001)

void xMbPortEventInit(void);

void xMbPortEventPost(const eMB_EVENT_TYPE _event);
void xMbPortEventPostNoInt(const eMB_EVENT_TYPE _event);

eMB_EVENT_TYPE xMbPortEventGet();

void * Get_modbus_poll_event();
void * Get_mb_rx_dque();
void Init_modbus_tasks();
void Activate_modbus_tasks();

/******************************************************************************/
/*      Serial port functions                                                 */
/******************************************************************************/
bool_t xMbPortSerialInit(const uint32_t _baudRate,
                         const uint8_t _dataBits,
                         const eMB_PARITY _parity);

void   vMbPortSerialEnable(const bool_t _xRxEnable,
                           const bool_t _xTxEnable);

bool_t vMbPortSerialIsTxRun();


/******************************************************************************/
/*      Timers functions                                                      */
/******************************************************************************/
bool_t xMbPortTimersInit(const uint16_t _timeout50us);

void   vMbPortTimersEnable(void);

void   vMbPortTimersDisable(void);


/******************************************************************************/
/*      Callback for the protocol stack                                       */
/******************************************************************************/
/*
 *  Callback function for the porting layer when a new byte is available.
 *
 *  Depending upon the mode this callback function is used by the RTU or
 *  ASCII transmission layers. In any case a call to xMbPortSerialGetByte()
 *  must immediately return a new character.
 *
 *  Return TRUE if a event was posted to the queue because
 *  a new byte was received. The port implementation should wake up the
 *  tasks which are currently blocked on the eventqueue.
 */


#ifdef __cplusplus
  PR_END_EXTERN_C
#endif


#endif